package com.redxiii.spike;

import java.util.Map;
import java.util.Set;

import com.redxiii.spike.api.communication.CommunicationType;
import com.redxiii.spike.api.pmi.PmiType;
import com.redxiii.spike.engine.CoreMain;
import com.redxiii.spike.engine.CoreSettings;
import com.redxiii.spike.pmi.commons.PmiSettings;
import com.redxiii.spike.pmi.commons.PmiSettings.DBType;
import com.redxiii.spike.pmi.redmine.RedmineHandler;
import com.redxiii.spike.pmi.trac.TracHandler;
import com.redxiii.spike.services.msn.MsnService;
import com.redxiii.spike.services.msn.MsnSettings;
import com.redxiii.spike.services.xmpp.XmppService;
import com.redxiii.spike.services.xmpp.XmppSettings;
import com.redxiii.spike.util.IMHandler;
import com.redxiii.spike.util.core.StorageSystem;
import com.redxiii.util.cli.CmdLineIO;

public class InstallC {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CmdLineIO io = new CmdLineIO();
		
		String instance = io.getString("Instance ID");
		
		CommunicationType communicationType = (CommunicationType) io.getEnum(CommunicationType.class, "Communication (XMPP, MSN)");
		String imUser = io.getString("IM User");
		String imPass = io.getString("IM Password");
		
		PmiType pmiType = (PmiType) io.getEnum(PmiType.class, "Project Management (Trac, Redmine)");
		DBType dbType = (DBType) io.getEnum(DBType.class, "Database (MySql, PosgreSql)");
		
		String dbHost = io.getString("DB Host");
		String dbSchema = io.getString("DB Schema");
		String dbUser = io.getString("DB User");
		String dbPass = io.getString("DB Password");
		
		StorageSystem.init(instance);
		StorageSystem storage = StorageSystem.getInstance();
		CoreSettings coreSettings = storage.load(CoreSettings.class, null, true);
		
		// Project Management
		Map<String, Class<?>> handlerAlias = coreSettings.getHandlerAlias();
		handlerAlias.clear();
		handlerAlias.put("@Trac", TracHandler.class);
		handlerAlias.put("@Redmine", RedmineHandler.class);
		handlerAlias.put("@IM", IMHandler.class);
		
		coreSettings.setHandlerAlias(handlerAlias);
		
		// Project Management - AutoStart Handler
		Set<Class<?>> handlerAutoStart = coreSettings.getHandlerAutoStart();
		PmiSettings pmiSettings = storage.load(PmiSettings.class, pmiType.name(), true);
		switch(pmiType) {
			case Redmine:
				handlerAutoStart.add(RedmineHandler.class);
				break;
			case Trac:
				handlerAutoStart.add(TracHandler.class);
				break;
		}
		
		pmiSettings.setDbType(dbType);
		pmiSettings.setDbHost(dbHost);
		pmiSettings.setDbName(dbSchema);
		pmiSettings.setDbLogin(dbUser);
		pmiSettings.setDbPassword(dbPass);
		pmiSettings.setAutoLoginOnly(true);
		
		// Communication Servive
		switch(communicationType) {
			case MSN:
				coreSettings.setService(MsnService.class);
				MsnSettings msnSettings = storage.load(MsnSettings.class, communicationType.name().toLowerCase(), true);
				msnSettings.setLogin(imUser);
				msnSettings.setPassword(imPass);
				break;
			case XMPP:
				coreSettings.setService(XmppService.class);
				XmppSettings xmppSettings = storage.load(XmppSettings.class, communicationType.name().toLowerCase(), true);
				xmppSettings.setLogin(imUser);
				xmppSettings.setPassword(imPass);
				break;
		}
		
		storage.persist();
		
		CoreMain.main(new String[]{instance});
	}

}
